package com.jdk8.demo.stream;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest4 {
    public static void main(String[] args) {
      //  Stream<String> stream1 = Stream.of("hello", "world", "hello world");
//        String[] strings = stream1.toArray((length -> new String[length]));
//        String[] strings = stream1.toArray(String[]::new);
//        Arrays.asList(strings).forEach(System.out::println);

        System.out.println("----------------------------");
        Stream<String> stream1 = Stream.of("hello", "world", "hello world");
        List<String> list = stream1.collect(Collectors.toList());
        list.forEach(System.out::println);

        // 转换成LinkedList
        System.out.println("-------------------");
        Stream<String> stream2 = Stream.of("hello", "world", "hello world");
        LinkedList<Object> list5 = stream2.collect(LinkedList::new, (list1, list2) -> list1.add(list2), (list3, list4) -> list3.addAll(list4));
        list5.forEach(System.out::println);


        //另外一种创建List的方法
        System.out.println("----------------");
        Stream<String> stream3 = Stream.of("hello", "world", "hello world");
        ArrayList<String> stream3List = stream3.collect(Collectors.toCollection(ArrayList::new));
        stream3List.forEach(System.out::println);

        //转换成Set集合
        System.out.println("-----------------");
        Stream<String> stream4 = Stream.of("hello", "world", "hello world");
        HashSet<String> stream4Set = stream4.collect(Collectors.toCollection(HashSet::new));
        stream4Set.forEach(System.out::println);

        //字符串的拼接
        System.out.println("-----------------");
        Stream<String> stream5 = Stream.of("hello", "world", "hello world");
        System.out.println(stream5.collect(Collectors.joining("-")));


    }
}